<?php 
	include '../init.php';

	// 接收 执行标志
	$bz = $_GET['bz'];

	$goods_list = $_POST;

	switch ($bz) {
		case 'add':

		$stock = $_POST['stock'];
		$price = $_POST['price'];
		// var_dump($price);

				//判断传输值是否为空
				foreach($goods_list as $v){
					if( empty($v)){
						notice('表单不能为空');
					}
				}

				if($price <=0){
					notice('价格不能为负数');
				}
				
				if($stock <=0){
					notice('库存不能为负');
				}

				//判断文件是否上传成功，失败则直接退出	
				if(  key($_FILES) == null || $_FILES[key($_FILES)]['error'] > 0   ){
					notice('上传文件过大或者其他原因, 请上传图片...');
				}

				$field_key = '';
				$field_value = '';
				// 拼接sql语句
				foreach($goods_list as $k => $v){
					$field_key .= ' `'.$k.'` ,';
					$field_value .= ' "'.htmlentities($v).'", ';
				}

				$field_key .= '`addtime`';
				$field_value .= time();

				$sql = 'insert into goods('.$field_key.')  values('.$field_value.')';

				$ins_id = zsg($sql);
				//成功添加商品，则在添加图片
				if($ins_id){
					//上传商品图片，返回图片名
					$fiilename = up_img(key($_FILES), ROOT.'uploads/');
					$sql ='insert into goodsImg(`gid`,`icon`,`face`) values("'.$ins_id.'","'.$filename.'",1)';	
					zsg($sql);
					notice('成功添加商品','index.php');
				}else{
					notice('添加商品失败');
				}	

			break;

		case 'edit':
			$field = '';
			// 判断表单是否为空, 空的话跳过, 不为空 判断密码一致, 拼接sql语句
			foreach($goods_list as $k => $v){
				// 表单不为空	
					$stock = $_POST['stock'];
				$price = $_POST['price'];
			if($price <=0){
					notice('价格不能为负数');
				}
				
				if($stock <=0){
					notice('库存不能为负');
				}
			$field .= '`'.$k.'` = "'.htmlentities($v).'",';
			}
			
			$field = rtrim($field,',');
			
			$sql = 'update goods set '.$field.' where id='.$goods_list['id'];
			// echo $sql;exit;
			if(zsg($sql)){
				notice('编辑成功, 正在前往商品列表....','edit.php');
			}else{
				notice('编辑失败,请重新来过');
			}

		 	

			break;
		case 'del':
				// 删除图片, 不仅删数据库里的图片名, 还要删除文件夹的里的图片
		$id = $_GET['id'];

				// 先通过id 查询头像
				// 如果有头像, 先删除头像
				// 自己写
                $sql='select `icon` from goodsimg where `gid`='.$id;
                $result=query($sql);
                // var_dump($result);exit;
                foreach($result as $k => $v){
	                if($v['icon']){
	                	$iconname=$v['icon'];
						$iconpath=ROOT.'uploads/';
						$iconpath.=substr($iconname,0,4).'/';
						$iconpath.=substr($iconname,4,2).'/';
						$iconpath.=substr($iconname,6,2).'/';

						unlink($iconpath.'50_'.$iconname);
						unlink($iconpath.'240_'.$iconname);
						unlink($iconpath.'360_'.$iconname);
						unlink($iconpath.$iconname);
	                }
                }
				$sql = 'delete from goods where `id`='.$id;
				// var_dum($sql);exit;
                $sqll= 'delete from goodsimg where `gid`='.$id;
				if(zsg($sql)&&zsg($sqll)){
					header('location:'.$_SERVER['HTTP_REFERER']);
				}else{
					notice('删除失败');
				}
			break;


			case 'delimg':
		     $id=$_GET['id'];
		      $sql='select `icon`,`face` from goodsimg where `id`='.$id;
                $result=query($sql);                
	                if($result[0]['face']==2){
	                	$iconname=$result[0]['icon'];
						$iconpath=ROOT.'uploads/';
						$iconpath.=substr($iconname,0,4).'/';
						$iconpath.=substr($iconname,4,2).'/';
						$iconpath.=substr($iconname,6,2).'/';

						unlink($iconpath.'50_'.$iconname);
						unlink($iconpath.'240_'.$iconname);
						unlink($iconpath.'360_'.$iconname);
						unlink($iconpath.$iconname);
						$sql = 'delete from goodsimg where `id`='.$id;
						zsg($sql);
						header('location:'.$_SERVER['HTTP_REFERER']);
	                }else{
					notice('封面图片不允许删除,请重试');
				}



		case 'face':
			//先接受 当前用户id
				$id = $_GET['id'];
				$gid = $_GET['gid'];

				$sql = 'update goodsImg set `face` = 2 where gid = '.$gid;

				if(zsg($sql)){
					$sql = 'update goodsImg set `face` = 1 where id='.$id;
					zsg($sql);
					header('location:'.$_SERVER['HTTP_REFERER']);
				}else{
					notice('状态更新失败');
				}
				break;

		case 'add_img':
			//接收商品的id
			$gid = $_POST['gid'];	

			// 判断文件是否上传成功, 失败则直接退出
			if(  key($_FILES) == null || $_FILES[key($_FILES)]['error'] > 0   ){
					notice('上传文件过大或者其他原因, 请上传图片...');
				}

			// 上传商品图片, 返回图片名
			$filename = up_img(key($_FILES), ROOT.'uploads/');

			$sql = 'insert into goodsImg(`gid`,`icon`)  values("'.$gid.'","'.$filename.'")';

			if(zsg($sql)){
				notice('添加成功');
			}else{
				notice('添加失败');
			}
			break;

			case 'up':
				// 先接收 当前用户的id
				$id = $_GET['id'];

				$up = $_GET['up']==1?2:1;

				$sql = 'update goods set `up` = '.$up .' where id = '.$id;

				if(zsg($sql)){
					header('location:'.$_SERVER['HTTP_REFERER']);
				}else{
					notice('状态更新失败');
				}

			break;
			case 'hot':
				// 先接收 当前用户的id
				$id = $_GET['id'];

				$hot = $_GET['hot']==1?2:1;

				$sql = 'update goods set `hot` = '.$hot .' where id = '.$id;

				if(zsg($sql)){
					header('location:'.$_SERVER['HTTP_REFERER']);
				}else{
					notice('状态更新失败');
				}

			break;
		default:
			# code...
			break;
	}

 ?>